package com.sugon.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.RandomUtil;
import com.sugon.dao.MaterialRecordDao;
import com.sugon.entity.MaterialRecordEntity;
import com.sugon.entity.SpareStorePosEntity;
import com.sugon.service.MaterialRecordService;
import com.sugon.service.SpareStorePosService;
import com.sugon.utils.Constant;
import org.apache.shiro.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 领料单详细Service实现类
 *
 * @author sugon
 * @date 2020-01-08 08:53:20
 */
@Service("materialRecordService")
public class MaterialRecordServiceImpl implements MaterialRecordService {
    @Autowired
    private MaterialRecordDao materialRecordDao;
    @Autowired
    private SpareStorePosService spareStorePosService;

    /**
     * @param id 主键
     * @return
     */
    @Override
    public MaterialRecordEntity queryObject(String id) {
        return materialRecordDao.queryObject(id);
    }

    /**
     * @param map 参数
     * @return
     */
    @Override
    public List<MaterialRecordEntity> queryList(Map<String, Object> map) {
        return materialRecordDao.queryList(map);
    }

    /**
     * @param map 参数
     * @return
     */
    @Override
    public int queryTotal(Map<String, Object> map) {
        return materialRecordDao.queryTotal(map);
    }

    /**
     * @param map
     * @return
     */
    @Override
    public int listByStoreTotal(Map<String, Object> map) {
        return materialRecordDao.listByStoreTotal(map);
    }

    /**
     * @param map
     * @return
     */
    @Override
    public int listByToolsStoreTotal(Map<String, Object> map) {
        return materialRecordDao.listByToolsStoreTotal(map);
    }

    /**
     * @param materialRecord 实体
     * @return
     */
    @Override
    public int save(MaterialRecordEntity materialRecord) {
        materialRecord.setId(RandomUtil.randomString(32));
        // 默认未删除
        materialRecord.setDelFlag(Constant.IS_NOT_DEL_INT);
        materialRecord.setActualNum(materialRecord.getNum());
        // 关联后开始计算
        materialRecord.setReplaceNum(0);
        return materialRecordDao.save(materialRecord);
    }

    /**
     * @param materialRecord 实体
     * @return
     */
    @Override
    public int update(MaterialRecordEntity materialRecord) {
        return materialRecordDao.update(materialRecord);
    }

    /**
     * @param id
     * @return
     */
    @Override
    public int delete(String id) {
        return materialRecordDao.delete(id);
    }

    /**
     * @param ids
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteBatch(String[] ids) {
        int totalRows = 0;
        if (ArrayUtil.isNotEmpty(ids)) {
            for (String id : ids) {
                MaterialRecordEntity materialRecord = materialRecordDao.queryObject(id);
                Assert.notNull(materialRecord, "领料清单不存在！");
                int rows = materialRecordDao.delete(id);
                if (rows > 0) {
                    totalRows += rows;
                    spareStorePosService.realtimeUpdateStorePosStock(materialRecord.getPostionId());
                }
            }
        }
        return totalRows;
    }

    /**
     * @param params
     * @return
     */
    @Override
    public List<MaterialRecordEntity> queryByMaterAndSpare(Map<String, Object> params) {
        return materialRecordDao.queryByMaterAndSpare(params);
    }

    /**
     * @param mater 领料单号
     * @return
     */
    @Override
    public List<MaterialRecordEntity> queryByMater(String mater) {
        Map<String, Object> param = MapUtil.newHashMap(1);
        param.put("mater", mater);
        return queryByMaterAndSpare(param);
    }

    /**
     * @param params
     * @return
     */
    @Override
    public List<MaterialRecordEntity> queryReport(Map<String, Object> params) {
        return materialRecordDao.queryReport(params);
    }

    /**
     * @param map
     * @return
     */
    @Override
    public List<Map<String, Object>> listByStore(Map<String, Object> map) {
        return materialRecordDao.listByStore(map);
    }

    /**
     * @param map
     * @return
     */
    @Override
    public List<Map<String, Object>> listByToolsStore(Map<String, Object> map) {
        List<Map<String, Object>> mapList = new ArrayList<>();
        //获取领料单所辖的领料清单
        List<MaterialRecordEntity> materialRecordList = materialRecordDao.queryList(map);
        //根据领料清单获取出获取库存库位信息
        if (materialRecordList.size() > 0) {
            for (MaterialRecordEntity mre : materialRecordList) {
                Map<String, Object> params = new HashMap<>();
                params.put("spareCode", mre.getSpareCode());
                params.put("storeId", mre.getStore());
                List<SpareStorePosEntity> posList = spareStorePosService.queryList(params);
            }
        }
        return null;
    }

    /**
     * @param materialRecord
     * @return
     */
    @Override
    public int add(MaterialRecordEntity materialRecord) {
        int rows = 0;
        Map<String, Object> params = new HashMap<>(3);
        params.put("mater", materialRecord.getMaterial());
        params.put("postionId", materialRecord.getPostionId());
        params.put("spareCode", materialRecord.getSpareCode());
        List<MaterialRecordEntity> list = queryByMaterAndSpare(params);
        if (CollectionUtil.isNotEmpty(list)) {
            MaterialRecordEntity mre = list.get(0);
            mre.setNum(materialRecord.getNum());
            rows = update(mre);
        } else {
            rows = save(materialRecord);
        }
        return rows;
    }
}
